/*The MIT License

Copyright (c) <2008> <Samir Menon>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

#include "CCaKNaNeuron.h"

namespace bis_neuron
{

CCaKNaNeuron::CCaKNaNeuron() : CKNaNeuron()
{
	r_ca = DEFAULT_R_CA;
	i_ca = DEFAULT_I_CA;
	tau_ca = DEFAULT_TAU_CA;
//	i_ca_max = DEFAULT_I_CA_MAX;
	delta_i_ca = DEFAULT_DELTA_I_CA;
	i_ca_hold = 0.0;
	r=1.2;//For faster bursting
}

CCaKNaNeuron::CCaKNaNeuron(float arg_tau_ca, float arg_i_ca_max, float arg_delta_i_ca, 
							float arg_r_ca, 
							float arg_tau_k, float arg_delta_gk, 
							float arg_sigma, float arg_n) 
			: CKNaNeuron()
{
	//Inherited
	sigma = arg_sigma; 
	n = arg_n;
	g_k = 0.0;
	t_last_spike = 0;
	tau_k = arg_tau_k;
	delta_gk = arg_delta_gk;
	//For Ca
	i_ca = DEFAULT_I_CA;
	r_ca = arg_r_ca;	
	tau_ca = arg_tau_ca;
//	i_ca_max = arg_i_ca_max;
	delta_i_ca = arg_delta_i_ca;
	i_ca_hold = 0.0;
	r=1.2;//For faster bursting
}

CCaKNaNeuron::~CCaKNaNeuron()
{
}

bool CCaKNaNeuron::fire()
{//Progress one time-cycle
	if(execution_flag < 1) {return false;}
	if(clock == NULL) {	execution_flag = -1; return false;}
	return update_vm();
	//update_vm_burst(); //Relies on supra-threshold voltage reset for bursting		
}

inline bool CCaKNaNeuron::update_vm()
{
	//Sodium channel excitability dependent n and sigma
	//TODO: Replace rand() with randn() where
	//randn() gives a normally distributed var [0,1]
	
	assert(tau_k != 0);
	assert(tau_ca != 0);
	
	v_na=0.0+exp(sigma*(rand()/RAND_MAX)*n); 
	float g=0.0, temp;
	float vm_delta = 0.0;
	
	if (vm >= vm_max){//Spike	
		vm = 0;
		log_data();
		log_spike();
		temp = clock->get_time();
		temp = -(temp - t_last_spike);
		g_k = (g_k* exp(temp/tau_k) + delta_gk);
		i_ca = i_ca_hold + delta_i_ca;
//		i_ca = i_ca_hold + 0.25 * (i_ca_max - i_ca);
		t_last_spike = clock->get_time();
		return true;//Spike
	}
	else if (vm < vm_min){ //Membrane potential underflow
		vm = vm_min;
	}
	else{//Increment membrane potential -> Cubic growth
		temp = clock->get_time();
		temp = -(temp - t_last_spike);
		g_k_hold = g_k * exp(temp/tau_k);
		g = g + g_k_hold;
		i_ca_hold = i_ca * exp(temp/tau_ca);//Exponential Ca decay
		
		vm_delta =(-vm*(1+g) + r + v_na * pow(vm,3)/3);
		vm_delta = SCALING_FACTOR * vm_delta;
		vm = vm + vm_delta + (i_ca_hold*r_ca);
		if (vm >= vm_max){//Spike
			vm = vm_max;
		}
		log_data();
	} 
	return false;//No spike
}

void CCaKNaNeuron::log_data(){
		string log_dump;
		if(data_spooler == NULL) { return;	}		
		if(true == log_data_flag){
			data_log_stream<<vm<<"   "<<g_k_hold<<"	"<<i_ca_hold<<"	"<<t_last_spike;
			log_dump.clear();
			log_dump += data_log_stream.str();
			data_spooler->spool_data(neuron_id,&log_dump);
			data_log_stream.str("");
		}		
	}


//Uses supra-threshold membrane voltage reset for bursting
inline bool CCaKNaNeuron::update_vm_burst()
{
	//Sodium channel excitability dependent n and sigma
	//TODO: Replace rand() with randn() where
	//randn() gives a normally distributed var [0,1]
	
	assert(tau_k != 0);
	
	v_na=0.0+exp(sigma*(rand()/RAND_MAX)*n); 
	float g=0.0, temp;
	float vm_delta = 0.0;
	
	if (vm >= vm_max){//Spike	
		vm = 3;
		log_data();
		log_spike();
		temp = clock->get_time();
		temp = -(temp - t_last_spike);
		g_k = (g_k* exp(temp/tau_k) + delta_gk);
		t_last_spike = clock->get_time();
		return true;//Spike
	}
	else if (vm < vm_min){ //Membrane potential underflow
		vm = vm_min;
	}
	else{//Increment membrane potential -> Cubic growth
		temp = clock->get_time();
		temp = -(temp - t_last_spike);
		g_k_hold = g_k * exp(temp/tau_k);
		g = g + g_k_hold;
		
		vm_delta =(-vm*(1+g) + r + v_na * pow(vm,3)/3);
		vm_delta = SCALING_FACTOR * vm_delta;
		vm = vm + vm_delta;
		if (vm >= vm_max){//Spike
			vm = vm_max;
		}
		log_data();
	} 
	return false;//No Spike
}
}
